var inputEvent = new Event('input', {
    bubbles: true,
    cancelable: true
});
let refreshBtn = null;
let mday = 21;
for (let use of document.querySelectorAll('use')) {
    if (use.getAttribute('xlink:href') === '#icon-sync') {
        refreshBtn = use.parentElement.parentElement;
        Refresh();
        break;
    }
}

function Refresh() {
    if (refreshBtn) {
        refreshBtn.click();
    }
    for (let inputDate of document.querySelectorAll("input")) {
        if (inputDate.placeholder === "选择日期") {
            inputDate.focus();
            document.body.click();
        }
    }
    let array = document.querySelectorAll('div');
    for (let i = 0; i < array.length; i++) {
        if (array[i].innerHTML.replace(/\s/g, '') === '<span>' + mday + '</span>') {
            if (array[i].parentElement.classList.contains('available')) {
                array[i].click();
                autoFillInfo();
                break;
            }
        }
        if (i == array.length - 1) {
            setTimeout(() => {
                Refresh();
            }, 200);
        }
    }
}

function autoFillInfo() {
    const aaa = setInterval(() => {
        document.querySelectorAll("span").forEach((ele) => {
            //选择表演场次
            if (ele.innerText === '14:00-15:00') {
                clearInterval(aaa);
                ele.click();

                //输入预定席位
                let bbb = setInterval(() => {
                    var inputs = document.querySelectorAll("input");
                    var isVip = true;
                    for (let i = 0; i < inputs.length; i++) {
                        if (inputs[i].placeholder == "请输入数量") {
                            if (bbb) {
                                clearInterval(bbb);
                            }
                            if (isVip) {
                                inputs[i].value = "0";
                                inputs[i].dispatchEvent(inputEvent);
                                isVip = false;
                            } else {
                                inputs[i].value = "52";
                                inputs[i].dispatchEvent(inputEvent);
                            }
                        }
                    }
                }, 500);

                // 选择团队类型
                let ccc = setInterval(() => {
                    document.querySelectorAll("span").forEach((ele) => {
                        if (ele.innerText === "成人团") {
                            clearInterval(ccc);
                            ele.click();
                            for (let eee of document.querySelectorAll("span")) {
                                if (eee.innerText === "立即预订") {
                                    eee.click();
                                    break;
                                }
                            }
                        }

                    })
                }, 500);
            }
        })
    }, 500);
}
